package com.asha.md360player4android.vr.objects;

import android.content.Context;

import com.asha.vrlib.CalibrateInfo;
import com.asha.vrlib.objects.MDAbsObject3D;
import com.asha.vrlib.objects.MDObject3DHelper;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

/**
 * Created by hzqiujiadi on 16/6/26.
 * hzqiujiadi ashqalcn@gmail.com
 */
public class MDCircleExtra extends FYMDAbsObject3D {

    private static final String TAG = "MDCircleExtra";
    public MDCircleExtra() {
    }

    @Override
    void reloadObject(CalibrateInfo info, Context context) {
        MDObject3DHelper.loadObj(context, this);
    }

    @Override
    protected void executeLoad(Context context) {
        CalibrateInfo info = getCalibrateInfo(context);
        if (null != info && info.getOriginWidth() > 0 && info.getOriginHeight() > 0) {
            float origin_width = info.getOriginWidth();
            float origin_height = info.getOriginHeight();
            float radius = info.getRadius() * origin_height;
//            float center_x = info.getCenterX();
//            float center_y = info.getCenterY();
            float offset_x = info.getOffsetX();//(2 * center_x - origin_width) / origin_width;
            float offset_y = info.getOffsetY();//(2 * center_y - origin_height) / origin_height;
            float n_radius_y = radius / origin_height / 2f;
            float n_radius_x = radius / origin_width / 2f;
            generateMesh(this, offset_x, offset_y, 180, n_radius_x, n_radius_y, 480f / 854f / 2f, 0.5f);
        } else {
            generateMesh(this, 0f, 0f, 180, 480f / 854f / 2f, 0.5f, 480f / 854f / 2f, 0.5f);
        }
    }

    private void generateMesh(MDAbsObject3D object3D, float offset_x, float offset_y, int sectors, float radius_x, float radius_y, float s_radius_x, float s_radius_y) {
        final int nodeCount = sectors + 1;

        float vertexs[] = new float[nodeCount * 3];
        float texcoords[] = new float[nodeCount * 2];
        short indices[] = new short[sectors * 3];

        int offset_texcoord = 0;
        int offset_vertex = 0;
        //for center point
        texcoords[offset_texcoord++] = offset_x + 0.5f;
        texcoords[offset_texcoord++] = offset_y + 0.5f;
        vertexs[offset_vertex++] = 0f;
        vertexs[offset_vertex++] = 0f;
        vertexs[offset_vertex++] = 0;

        //for circle point
        for (int i = 0; i < sectors; i++) {
            float angleInRadians = ((float) (i) / (float) sectors) * ((float) Math.PI * 2f);

            texcoords[offset_texcoord++] = offset_x + 0.5f + radius_x * (float) (Math.sin(angleInRadians));
            texcoords[offset_texcoord++] = offset_y + 0.5f + radius_y * (float) (Math.cos(angleInRadians));

            vertexs[offset_vertex++] = s_radius_x * (float) Math.sin(angleInRadians);//480f / 854f / 2 * (float) Math.sin(angleInRadians);
            vertexs[offset_vertex++] = s_radius_y * (float) Math.cos(angleInRadians);//0.5f * (float) Math.cos(angleInRadians);
            vertexs[offset_vertex++] = 0;
        }

        for (int i = 0; i < sectors; i++) {
            indices[i * 3] = 0;
            indices[i * 3 + 1] = (short) (i + 1);
            indices[i * 3 + 2] = (short) (i + 2);
        }

        indices[indices.length - 1] = 1;

        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 4 bytes per float)
                vertexs.length * 4);
        bb.order(ByteOrder.nativeOrder());
        FloatBuffer vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(vertexs);
        vertexBuffer.position(0);

        // initialize vertex byte buffer for shape coordinates
        ByteBuffer cc = ByteBuffer.allocateDirect(
                texcoords.length * 4);
        cc.order(ByteOrder.nativeOrder());
        FloatBuffer texBuffer = cc.asFloatBuffer();
        texBuffer.put(texcoords);
        texBuffer.position(0);

        // initialize byte buffer for the draw list
        ByteBuffer dlb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 2 bytes per short)
                indices.length * 2);
        dlb.order(ByteOrder.nativeOrder());
        ShortBuffer indexBuffer = dlb.asShortBuffer();
        indexBuffer.put(indices);
        indexBuffer.position(0);

        object3D.setIndicesBuffer(indexBuffer);
        object3D.setTexCoordinateBuffer(0, texBuffer);
        object3D.setTexCoordinateBuffer(1, texBuffer);
        object3D.setVerticesBuffer(0, vertexBuffer);
        object3D.setVerticesBuffer(1, vertexBuffer);
        object3D.setNumIndices(indices.length);
    }
}
